package com.zenchn.controller;

import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.ImmutableMap;
import com.zenchn.entity.HttpDataResult;
import com.zenchn.entity.PlatformConfigVO;
import com.zenchn.entity.SysConfig;
import com.zenchn.model.*;
import com.zenchn.service.DictService;
import com.zenchn.service.SysFileService;
import com.zenchn.service.WebStyleConfigService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 登录页相关配置控制层
 * @author dingxinfa
 * @date 2020-04-20
 */
@RestController
@RequestMapping(value = "loginPage")
@Api(value = "登录页相关配置接口",tags="登录页-参数接口")
public class LoginPageController {


    @Resource
    private WebStyleConfigService webStyleConfigService;

    @Resource
    private DictService dictService;

    @Resource
    private SysFileService sysFileService;

    @Resource
    private SysConfig sysConfig;

    @Value("${custom.platform.version}")
    private String platformVersion;

    /**
     * 获取登录账号 对应机构平台自定义配置信息
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    @ApiOperation(value = "当前平台自定义配置信息", notes = "当前平台自定义配置信息")
    @GetMapping(value = "/platformConfig")
    @ApiImplicitParam(dataType = "String", name = "accessParameter", value = "访问参数")
    public ResponseEntity<HttpDataResult<PlatformConfigVO>> platformConfig(
            @RequestParam(required = false, value = "accessParameter") String accessParameter,
            HttpServletRequest request, HttpServletResponse response) throws Exception {
        HttpDataResult<PlatformConfigVO> result = new HttpDataResult<>(request.getRequestURI());

        PlatformConfigVO platformConfigVO=new PlatformConfigVO();
        platformConfigVO.setPlatformVersion(platformVersion);

        TSysWebStyleConfig webStyleConfig=new TSysWebStyleConfig();


        TSysWebStyleConfigExample webStyleConfigExample=new TSysWebStyleConfigExample();
        if (null!=accessParameter && !"".equals(accessParameter.trim())){
            webStyleConfigExample.createCriteria().andAccessParameterEqualTo(accessParameter.trim());
            webStyleConfigExample.setOrderByClause("style_config_id asc limit 1");
            List<TSysWebStyleConfig> webStyleConfigList=webStyleConfigService.selectByExample(webStyleConfigExample);
            if (null==webStyleConfigList|| webStyleConfigList.isEmpty()) {
                webStyleConfigExample=new TSysWebStyleConfigExample();
                webStyleConfigExample.createCriteria().andIsGenericEqualTo(true);
                webStyleConfigExample.setOrderByClause("style_config_id asc limit 1");
                webStyleConfigList=webStyleConfigService.selectByExample(webStyleConfigExample);
            }
            if (null!=webStyleConfigList && !webStyleConfigList.isEmpty()){
                webStyleConfig=webStyleConfigList.get(0);
            }
        }else {
            webStyleConfigExample.createCriteria().andIsGenericEqualTo(true);
            webStyleConfigExample.setOrderByClause("style_config_id asc limit 1");

            List<TSysWebStyleConfig> webStyleConfigList=webStyleConfigService.selectByExample(webStyleConfigExample);
            if (null!=webStyleConfigList && !webStyleConfigList.isEmpty()){
                webStyleConfig=webStyleConfigList.get(0);
            }
        }
        
        platformConfigVO.setPlatformName(webStyleConfig.getPlatformName());
        platformConfigVO.setIconFileId(webStyleConfig.getIconFileId());
        platformConfigVO.setLogoFileId(webStyleConfig.getLogoFileId());
        platformConfigVO.setCopyright(webStyleConfig.getCopyright());
        platformConfigVO.setPlatformStyle(webStyleConfig.getPlatformStyle());
        platformConfigVO.setAccessParameter(webStyleConfig.getAccessParameter());

        List<String>  carouselImageIds = JSONObject.parseArray(webStyleConfig.getCarouselImageIds(), String.class);

        if (null != platformConfigVO.getIconFileId() && !"".equals(platformConfigVO.getIconFileId())) {
            TSysFile sf = sysFileService.selectByPrimaryKey(platformConfigVO.getIconFileId());
            platformConfigVO.setIconFileUrl(sysConfig.getCurrentUseFormalFileUrl() + "/" + sf.getFilePath());
        }

        if (null != platformConfigVO.getLogoFileId() && !"".equals(platformConfigVO.getLogoFileId())) {
            TSysFile sf = sysFileService.selectByPrimaryKey(platformConfigVO.getLogoFileId());
            platformConfigVO.setLogoFileUrl(sysConfig.getCurrentUseFormalFileUrl() + "/" + sf.getFilePath());
        }
        if (null != carouselImageIds&& !carouselImageIds.isEmpty()) {
            TSysFileExample fileExample=new TSysFileExample();
            fileExample.createCriteria().andFileIdIn(carouselImageIds);
            List<TSysFile> fileList=sysFileService.selectByExample(fileExample);

            List<Map<String,String>> carouselImageList = new ArrayList<>();
            fileList.forEach(file ->{
                String url=sysConfig.getCurrentUseFormalFileUrl() + "/" + file.getFilePath();
                carouselImageList.add(ImmutableMap.of("fileId",file.getFileId(),"url",url));
            });

            platformConfigVO.setCarouselImageList(carouselImageList);
        }


        result.setData(platformConfigVO);

        result.setMessage("获取平台配置成功.");
        result.setStatus(HttpStatus.OK.value());
        return new ResponseEntity<>(result, HttpStatus.OK);
    }

}
